/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package freechoicepn.pflow;

import freechoicepn.datamodel.*;
import java.util.ArrayList;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Tomas
 *
 * Z nacitaneho xml dokumentu rozparsuje nas datamodel
 */
public class PflowMiner {

    private ArrayList<Place> places;
    private ArrayList<Transition> transitions;
    private ArrayList<Arc> arcs;

    public PflowMiner(PflowLoader loader) {
        
        places = new ArrayList();
        transitions = new ArrayList();
        arcs = new ArrayList();
        
        parsePlaces(loader.getPlaceNodes());
        parseTransitions(loader.getTransitionNodes());
        parseArcs(loader.getArcNodes());
        
        // pridanie referencii
        setReferences();
    }

    private void parsePlaces(NodeList placeNodes) {

        for (int i = 0; i < placeNodes.getLength(); i++) {
            Node node = placeNodes.item(i);

            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;        
                
                int id = Integer.parseInt(element.getElementsByTagName("id").item(0).getTextContent());
                String label = element.getElementsByTagName("label").item(0).getTextContent();
                int tokens = Integer.parseInt(element.getElementsByTagName("tokens").item(0).getTextContent());
                boolean isStatic = Boolean.parseBoolean(element.getElementsByTagName("isStatic").item(0).getTextContent());
                
                Place p = new Place(id, label, tokens, isStatic);
                places.add(p);
            }
        }
    }
    
    private void parseTransitions(NodeList transitionNodes) {
        
        for (int i = 0; i < transitionNodes.getLength(); i++) {
            Node node = transitionNodes.item(i);
            
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                
                int id = Integer.parseInt(element.getElementsByTagName("id").item(0).getTextContent());
                String label = element.getElementsByTagName("label").item(0).getTextContent();
                
                Transition t = new Transition(id, label);
                transitions.add(t);
            }
        }
    }
    
    private void parseArcs(NodeList arcNodes) {
        
        for (int i = 0; i < arcNodes.getLength(); i++) {
            Node node = arcNodes.item(i);
            
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element) node;
                
                int sourceId = Integer.parseInt(element.getElementsByTagName("sourceId").item(0).getTextContent());
                int destinationId = Integer.parseInt(element.getElementsByTagName("destinationId").item(0).getTextContent());
                int multiplicity = Integer.parseInt(element.getElementsByTagName("multiplicity").item(0).getTextContent());
                
                Arc a = new Arc(sourceId, destinationId, multiplicity);
                arcs.add(a);
            }
        }
    }
    
    private void setReferences() {
        
        // prejdem vsetky sipky
        for (Arc arc : arcs) {

            for (Transition transition : transitions) {
                // ak sa najde sipka ktora konci v prechode
                if (transition.getId() == arc.getDestinationId()) {
                    // prejdem vsetky miesta
                    for (Place place : places) {
                        // pridam tento prechod ako vystupny prechod pre miesto v ktorom zacina sipka
                        if (place.getId() == arc.getSourceId()) {
                            place.addOutTransition(transition);
                            transition.addInPlace(place);
                            break;
                        }
                    }
                    break;
                }
            }
        }
    }

    public ArrayList<Place> getPlaces() {
        return places;
    }

    public ArrayList<Transition> getTransitions() {
        return transitions;
    }
    
    public ArrayList<Arc> getArcs() {
        return arcs;
    }

}